package com.wl.network.demo03;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;

/**
 * 文件上传
 */
public class TcpClient {
    public static void main(String[] args) throws Exception {
        //创建连接
        Socket socket=new Socket(InetAddress.getByName("192.168.1.4"),9000);
        //创建一个输出流
        OutputStream os = socket.getOutputStream();

        //读取文件
        FileInputStream fis = new FileInputStream(new File("C:\\Users\\Administrator.DESKTOP-6FVFRKA\\Pictures\\Saved Pictures\\avatar.jpeg"));
        //写出文件
        byte[] buffer = new byte[1024];
        int len;
        while ((len=fis.read(buffer))!=-1){
            os.write(buffer);
        }
        //告诉服务器，我结束了
        socket.shutdownOutput();

        //确定服务器接受完成，才可以断开
        InputStream inputStream = socket.getInputStream();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        byte[] buffer2 = new byte[1024];
        int len2;
        while ((len2=inputStream.read(buffer2))!=-1){
            baos.write(buffer2,0,len2);
        }

        System.out.println(baos.toString());
        //关闭资源
        baos.close();
        inputStream.close();
        fis.close();
        os.close();
        socket.close();
    }
}
